package com.xxx.store.service.impl;

import com.xxx.store.entity.Address;
import com.xxx.store.mapper.AddressMapper;
import com.xxx.store.service.AddressService;
import com.xxx.store.service.DistrictService;
import com.xxx.store.service.exception.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    //依赖于省份业务
    @Autowired
    private DistrictService districtService;

    @Value("${user.address.max-count}")
    private Integer maxCount;

    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        Integer count = addressMapper.countByUid(uid);
        if (count>maxCount){
            throw new AddressCountLimitException("地址数量超过限制");
        }
        String province = districtService.getNameByCode(address.getProvinceCode());
        String city = districtService.getNameByCode(address.getCityCode());
        String area = districtService.getNameByCode(address.getAreaCode());
        address.setProvinceName(province);
        address.setCityName(city);
        address.setAreaName(area);
        address.setUid(uid);
        Integer isDefault=count == 0 ? 1:0;
        address.setIsDefault(isDefault);
        address.setCreatedUser(username);
        address.setModifiedUser(username);
        Date date = new Date();
        address.setCreatedTime(date);
        address.setModifiedTime(date);
        Integer row = addressMapper.insert(address);
        if (row!=1){
            throw new InsertException("数据插入时异常");
        }
    }

    @Override
    public List<Address> getAddressByUid(Integer uid) {
        List<Address> list = addressMapper.findByUid(uid);
        for (Address address:list
             ) {
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);

            address.setCreatedTime(null);
            address.setCreatedUser(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);
        }
        return list;
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result==null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if (result.getUid()!=uid){
            throw new AccessDeniedException("非法数据访问");
        }
        Integer row = addressMapper.updateNonDefault(uid);
        if (row==-1){
            throw new UpdateException("更新产生异常");
        }
        Integer integer = addressMapper.updateDefaultByAid(aid, username, new Date());
        if (integer!=1){
            throw new UpdateException("更新产生异常");
        }
    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result==null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if (result.getUid()!=uid){
            throw new AccessDeniedException("非法数据访问");
        }
        Integer integer = addressMapper.deleteByAid(aid);
        if (integer!=1){
            throw new UpdateException("删除数据产生异常");
        }
    }

    @Override
    public Address getAddressNameByAid(Integer aid,Integer uid) {
        Address address = addressMapper.findByAid(aid);
        if (address==null){
            throw new AddressNotFoundException("收货地址不存在");
        }
        if (address.getUid()!=uid){
            throw new AccessDeniedException("非法数据访问");
        }
        address.setProvinceCode(null);
        address.setCityCode(null);
        address.setAreaCode(null);
        address.setCreatedUser(null);
        address.setModifiedUser(null);
        address.setCreatedTime(null);
        address.setModifiedUser(null);
        address.setModifiedTime(null);
        return address;
    }
}
